﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL.Admin;
 
using Entities;
using Entities.Admin;
using BLL.BLL;
 


namespace WindowsForms.Admin
{
    public partial class FormThemBoPhan : Form
    {
        public static BoPhan bophan;
        public bool state;
        int quyen=0;
       
        public FormThemBoPhan()
        {
            InitializeComponent();
          
       
        }

        private void btluu_Click(object sender, EventArgs e)

        {
            
            BoPhan bophan = new BoPhan();
          
                bophan.MaBoPhan = tbmabophan.Text;
                bophan.TenBoPhan = tbtenbophan.Text;
                bophan.PhanQuyen = quyen;
                if (!QuanLiBoPhan.KiemTra(bophan.MaBoPhan,BoPhan.Objects.All().ToList()))
                {
                    BoPhan.Objects.Add(bophan);
                    MessageBox.Show("Thêm Thành Công");
                }
                else
                {
                    DialogResult rs = MessageBox.Show("Đã có trong CSDL bạn muốn cập nhật không ?","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                    if (rs == DialogResult.Yes)
                    {
                        BoPhan.Objects.Update(bophan);
                        MessageBox.Show("Thêm Thất Bại");
                    }
                }
 
        }

        private void cbbquyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbquyen.SelectedIndex > 0)
            {
                quyen = cbbquyen.SelectedIndex;
 
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void GanGiaTriChoTestBoPhan(BoPhan bophan)
        {
            tbmabophan.Text = bophan.MaBoPhan;
            tbmabophan.ReadOnly = true;
            tbtenbophan.Text = bophan.TenBoPhan;
            //for (int i = 1; i < cbbquyen.Items.Count; i++)
            //    if (cbbquyen.Items[i].ToString() == bophan.PhanQuyen + "")
            cbbquyen.SelectedIndex = bophan.PhanQuyen;
            
        }

        private void FormThemBoPhan_Load(object sender, EventArgs e)
        {
            tbmabophan.Focus();
            cbbquyen.SelectedIndex = 0;
            if (state)
            {
                GanGiaTriChoTestBoPhan(bophan);
            }
        }

        private void tbmabophan_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(tbmabophan, "");

        }

        private void tbmabophan_Validating(object sender, CancelEventArgs e)
        {
                    
 
            if (QuanLiBoPhan.KiemTraRong(tbmabophan.Text))
            {
                errorProvider1.SetError(tbmabophan, "Không Được Để Trống");
               
                 
                e.Cancel=true;
                
            }
        }
 
       
    }
}
